namespace Tests
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Xml;
    using NUnit.Framework;
    using SPDeploy.Data;
    using SPDeploy.Library;
    using SPDeploy.Library.Exceptions;
    using SPDeploy.Library.Interfaces;

    [TestFixture]
    public class ConfigFixture
    {
        #region Setup/Teardown

        [SetUp]
        public void Init()
        {
        }

        #endregion

        #region Resources

        private static Stream GetEmbeddedResourceStream(string resourceName)
        {
            return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
        }

        private static Stream ValidXml
        {
            get
            {
                Stream res = GetEmbeddedResourceStream("Tests.Resources.Valid.xml");
                if (res == null)
                {
                    throw new Exception("Couldn't find test resource");
                }
                return res;
            }
        }

        private static Stream InvalidXml
        {
            get
            {
                Stream res = GetEmbeddedResourceStream("Tests.Resources.Invalid.xml");
                if (res == null)
                {
                    throw new Exception("Couldn't find test resource");
                }
                return res;
            }
        }

        private static Stream SchemaErrorXml
        {
            get
            {
                Stream res = GetEmbeddedResourceStream("Tests.Resources.SchemaError.xml");
                if (res == null)
                {
                    throw new Exception("Couldn't find test resource");
                }
                return res;
            }
        }

        #endregion

        [Test]
        public void LoadSuccessfulOnValidXml()
        {
            IDeploymentConfigLoader configLoader = new DeploymentConfigXmlLoader();
            Assert.DoesNotThrow(() => configLoader.Load(ValidXml));
        }

        [Test]
        public void LoadFailsOnIllegalXml()
        {
            IDeploymentConfigLoader configLoader = new DeploymentConfigXmlLoader();
            Assert.Throws(typeof(XmlException), () => configLoader.Load(InvalidXml));
        }

        [Test]
        public void LoadFailsOnSchemaValidationError()
        {
            IDeploymentConfigLoader configLoader = new DeploymentConfigXmlLoader();
            Assert.Throws(typeof(ValidationException), () => configLoader.Load(SchemaErrorXml));
        }

        [Test]
        public void WebApplicationLoadsFromXml()
        {
            IDeploymentConfigLoader configLoader = new DeploymentConfigXmlLoader();
            Deployment deployment = configLoader.Load(ValidXml);
            Assert.AreEqual("AppName", deployment.WebApplications[0].Name);
        }

        [Test]
        public void TryValidateAcceptsValidXml()
        {
            IDeploymentConfigLoader configLoader = new DeploymentConfigXmlLoader();
            Assert.AreEqual(true, configLoader.TryValidate(ValidXml));
        }

        [Test]
        public void TryValidateFailsOnIllegalXml()
        {
            IDeploymentConfigLoader configLoader = new DeploymentConfigXmlLoader();
            Assert.AreEqual(false, configLoader.TryValidate(InvalidXml));
        }

        [Test]
        public void TryValidateFailsOnSchemaValidationError()
        {
            IDeploymentConfigLoader configLoader = new DeploymentConfigXmlLoader();
            Assert.AreEqual(false, configLoader.TryValidate(SchemaErrorXml));
        }
    }
}